package fina2.dcs.mail.manager;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.mail.Address;
import javax.mail.internet.InternetAddress;

@SuppressWarnings("serial")
public class AttachInfo implements Serializable, Comparable<AttachInfo> {
	private Date receivedDate;
	private Map<String, byte[]> attachFiles;
	private String status;
	private String address;

	public AttachInfo() {

	}

	public AttachInfo(Date sentDate, Map<String, byte[]> attachFiles, String status, Address[] address) {
		this.receivedDate = sentDate;
		this.attachFiles = attachFiles;
		this.status = status;
		this.address = createMailAddress(address);
	}

	public Date getReceivedDate() {
		return receivedDate;
	}

	public void setReceivedDate(Date receivedDate) {
		this.receivedDate = receivedDate;
	}

	public Map<String, byte[]> getAttachFiles() {
		return attachFiles;
	}

	public void setAttachFiles(Map<String, byte[]> attachFiles) {
		this.attachFiles = attachFiles;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(Address[] address) {
		this.address = createMailAddress(address);
	}

	@Override
	public int compareTo(AttachInfo o) {
		return this.receivedDate.compareTo(o.receivedDate);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof AttachInfo) {
			AttachInfo a = (AttachInfo) obj;
			return this.receivedDate.equals(a.receivedDate);
		}
		return false;
	}

	private static String createMailAddress(Address[] addresses) {
		if (addresses.length > 0) {
			InternetAddress address = (InternetAddress) addresses[0];
			return address.getAddress();
		}
		return null;
	}

	@Override
	public String toString() {
		return "AttachInfo [sentDate=" + receivedDate + ", attachFiles count=" + attachFiles.size() + ", attachFiles name=" + Arrays.toString(attachFiles.keySet().toArray()) + ", status=" + status + ", address=" + address + "]";
	}
}
